{extend name="base"/}
{block name="resources"}
<link rel="stylesheet" type="text/css" href="SHOP_CSS/store_lists.css" />
<style>
	.ns-table-tab{margin-top: 0;}
	.img_size{width:100%;height:100%;}
</style>
{/block}
{block name="main"}
<input id="store_exit" type="hidden" value="{$store_is_exit}"/>
<div class="layui-collapse ns-tips">
	<div class="layui-colla-item">
		<h2 class="layui-colla-title">操作提示</h2>
		<ul class="layui-colla-content layui-show">
			<li>当前页面对门店的信息进行管理，可以添加门店，管理门店等。</li>
			<!--{if $store_is_exit == 1}
			<li>通过以下链接前往门店管理中心<a href="{:addon_url('store://store/login/login')}" target="_blank" class="ns-text-color"> {:addon_url('store://store/login/login')}</a></li>
			{/if}-->
		</ul>
	</div>
</div>
<!-- 搜索框 -->
<div class="ns-single-filter-box">
	<button class="layui-btn ns-bg-color" onclick="add()">添加门店</button>

	<div class="layui-form">
		<div class="layui-input-inline">
			<input type="text" name="keyword" placeholder="请输入门店名称" autocomplete="off" class="layui-input">
			<button type="button" class="layui-btn layui-btn-primary" lay-filter="search" lay-submit>
				<i class="layui-icon">&#xe615;</i>
			</button>
		</div>
	</div>
</div>

<div class="layui-tab ns-table-tab"  lay-filter="store_tab">
	<ul class="layui-tab-title">
		<li class="layui-this" lay-id="" lay-typer="">全部门店</li>
		<li lay-id="1" lay-type="1">营业中</li>
		<li lay-id="0" lay-type="1">休息中</li>
		<li lay-id="1" lay-type="2">已停业</li>
	</ul>
	<!--<div class="layui-tab-content">
		<table id="store_list" lay-filter="store_list"></table>
	</div>-->
	<div id="store_lists"></div>

	<div id="list_page"></div>
</div>

<!-- 门店详情 -->
<script type="text/html" id="store">
	<div class="ns-table-title">
		<div class="ns-title-pic">
			{{#  if(d.store_image){  }}
			<img layer-src src={{ns.img(d.store_image)}} alt="" class="img_size">
			{{#  }else{  }}
			<img layer-src src="{:img($default_img.default_store_img)}" alt="" class="img_size">
			{{#  }  }}
		</div>
		<div class="ns-title-content">
			<p class="layui-elip">店铺名称：{{d.store_name}}</p>
			<p class="layui-elip">联系方式：{{d.telphone}}</p>
		</div>
	</div>
</script>

<!-- 门店管理员 -->
<script type="text/html" id="store_admin">
	<div class="ns-table-title">
		<div class="ns-title-pic">
			<span>{{ d.username }}</span>
			<a style="color: #FF6A00;cursor: pointer;" onclick="resetStorePassword({{ d.store_id }})" >重置密码</a>
		</div>
	</div>
</script>

<!-- 操作 -->
<script type="text/html" id="action">
	<div class="ns-table-btn">
		<a class="layui-btn" lay-event="edit">编辑</a>
		{{# if(d.is_frozen) { }}
		<a class="layui-btn" lay-event="frozen">开启</a>
		{{# } else{ }}
		<a class="layui-btn" lay-event="frozen">关闭</a>
		{{# } }}
		<a class="layui-btn" lay-event="delete">删除</a>

		{if addon_is_exit('store')}
		<a class="layui-btn" lay-event="reset_pass">重置密码</a>
		{/if}
	</div>
</script>
{/block}
{block name="script"}
{if addon_is_exit('store')}
<!-- 重置密码弹框html -->
<script type="text/html" id="pass_change">
	<div class="layui-form" id="reset_pass" lay-filter="form">

		<div class="layui-form-item">
			<label class="layui-form-label mid"><span class="required">*</span>新密码：</label>
			<div class="layui-input-block">
				<input type="password" name="password" lay-verify="required" class="layui-input ns-len-mid new_pass" maxlength="18">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label mid"><span class="required">*</span>确认新密码：</label>
			<div class="layui-input-block">
				<input type="password" name="repassword" lay-verify="repass|required" class="layui-input ns-len-mid" maxlength="18">
			</div>
			<div class="ns-word-aux mid">请再一次输入密码，两次输入密码须一致</div>
		</div>

		<div class="ns-form-row mid">
			<button class="layui-btn ns-bg-color" lay-submit lay-filter="repass">确定</button>
			<button class="layui-btn layui-btn-primary" onclick="closePass()">返回</button>
		</div>

		<input class="reset-pass-id" type="hidden" name="store_id" value="{{d}}"/>
	</div>
</script>
{/if}
<script>
	var layer_pass,laytpl,table,form,element, laypage, list_count = 0, limit = 12, search_text = "", curr = 1, status = "", type = "", store_exit = $("#store_exit").val();
	layui.use(['form','element','laytpl'], function() {
		form = layui.form;
		element = layui.element;
		laytpl = layui.laytpl;
		repeat_flag = false; //防重复标识
        laypage = layui.laypage;
		form.render();

        getStoreLists(search_text, curr, status, type);
        pageList(list_count, limit, curr, status, type);

        //监听Tab切换，以改变地址hash值
        element.on('tab(store_tab)', function(){
            /*table.reload({
                page: {
                    curr: 1
                },
                where:{
                    'status':this.getAttribute('lay-id')
                }
            });*/
            var status = this.getAttribute('lay-id');
            var type = this.getAttribute('lay-type');
            getStoreLists(search_text, curr, status, type);
            pageList(list_count, limit, curr, status, type);
        });

        /*		table = new Table({
                    elem: '#store_list',
                    url: ns.url("shop/store/lists"),
                    cols: [
                        [{
                            title: '门店',
                            unresize: 'false',
                            width: '25%',
                            templet: '#store'
                        }, {
                            field: 'full_address',
                            title: '门店地址',
                            unresize: 'false',
                            width: '25%',
                            templet: function(data) {
                                return '<span title="'+data.full_address+data.address+'">'+data.full_address+data.address+'</span>'; //创建时间转换方法
                            }
                        },{
                            title: '管理员',
                            unresize: 'false',
                            width: '10%',
                            templet:'#store_admin',
                            hide: {$store_is_exit} ? false : true
                        },{
                            title: '创建时间',
                            unresize: 'false',
                            width: '10%',
                            templet: function(data) {
                                return ns.time_to_date(data.create_time); //创建时间转换方法
                            }
                        }, {
                            title: '门店状态',
                            unresize: 'false',
                            width: '10%',
                            templet: function(data) {
                                if (data.is_frozen == 1) {
                                    return '关闭';
                                } else {
                                    return '正常';
                                }
                            }
                        },{
                            title: '操作',
                            toolbar: '#action',
                            unresize: 'false',
                            width: '15%'
                        }]
                    ]
                });*/


		/**
		 * 删除
		 */
		function deleteCompany(store_id) {
			layer.confirm('门店已开始运营，确认要删除吗?', function() {
				$.ajax({
					url: ns.url("shop/store/deleteStore"),
					data: {store_id: store_id},
					dataType: 'JSON',
					type: 'POST',
					success: function(res) {
						layer.msg(res.message);
						repeat_flag = false;
						
						if (res.code == 0) {
							table.reload();
						}
					}
				});
			});
		}

		/**
		 * 删除
		 */
		function frozenStore(store_id, is_frozen) {
			var msg = '门店已开始运营，确认要关闭吗?';
			if(is_frozen == 1) {
				msg = '确定要开启该门店吗？';
			}
			layer.confirm(msg, function() {
				$.ajax({
					url: ns.url("shop/store/frozenStore"),
					data: {store_id:store_id, is_frozen:is_frozen},
					dataType: 'JSON',
					type: 'POST',
					success: function(res) {
						layer.msg(res.message);
						repeat_flag = false;

						if (res.code == 0) {
							table.reload();
						}
					}
				});
			});
		}
		
		/**
		 * 搜索功能
		 */
		form.on('submit(search)', function(data) {
			table.reload({
				page: {
					curr: 1
				},
				where: data.field
			});
		});

		/**
		 * 表单验证
		 */
		form.verify({
			repass: function(value) {
				if (value != $(".new_pass").val()) {
					return "输入错误,两次密码不一致!";
				}
			}
		});
		var repass_flag = false;
		form.on('submit(repass)', function(data) {

			if (repass_flag) return false;
			repass_flag = true;

			$.ajax({
				type: "POST",
				url: ns.url("shop/store/modifyPassword"),
				data: data.field,
				dataType: 'JSON',
				success: function(res) {
					layer.msg(res.message);
					repass_flag = false;
					if (res.code == 0) {
						layer.closeAll();
					}
				}
			});
		});

	});
	function closePass() {
		layer.close(layer_pass);
	}
	function add() {
		location.href = ns.url("shop/store/addStore");
	}

	/**
	 * 重置密码
	 */
	function resetStorePassword(data) {

		laytpl($("#pass_change").html()).render(data, function(html) {
			layer_pass = layer.open({
				title: '重置密码',
				skin: 'layer-tips-class',
				type: 1,
				area: ['550px'],
				content: html
			});
		});
	}

    function getStoreLists(search_text, curr, status, type){
        $("#store_lists").empty();
        $.ajax({
            type: "POST",
            url: ns.url("shop/store/lists"),
            data: {
                "page": curr,
                "page_size": limit,
                "search_text": search_text,
                "status" : status,
                "type" : type
            },
            dataType: 'JSON',
            async: false,
            success: function(res) {
                if (res.code == 0) {
                    var data = res.data.list;
                    list_count = res.data.count;
                    if(!data.length){
                        var empty = "<div class='empty' style='text-align: center;margin-top: 30px;color: #999'>无数据！</div>";
                        $("#store_lists").html(empty);
                    }

                    var html = "";
                    for (var i=0; i<data.length; i++) {
                        var d = data[i];
                        if (d.is_frozen == 1) {
                            html += '<div class="ns-store-li">';
                        } else {
                            html += '<div class="ns-store-li ns-border-color">';
                        }
                        html += '<p class="ns-store-name"><span class="store-name">'+ d.store_name +'</span>';
                        if(d.is_frozen == 1){
                            html += '<span class="type-name-cloe">停业</span>';
                        }else{
                            if(d.status == 0){
                                html += '<span class="type-name">休息</span>';
                            }else{
                                html += '<span class="type-name">正常</span>';
                            }
                        }
                        var telphone = d.telphone ? d.telphone : "暂无";

                        html += '</p>';
                        html += '<p class="ns-store-time ns-user-name">管理员：'+ d.username +'<a class="edit-password" onclick="resetStorePassword('+ d.store_id +')">重置密码</a></p>';
                        html += '<p class="ns-store-time">联系方式：'+ telphone +'</p>';
                        html += '<p class="ns-store-time">地址：'+ d.full_address +' '+ d.address +'</p>';
                        html += '<div class="ns-store-operation">';
                        html += '<span class="operation-type" onclick="editStore('+ d.store_id +')">编辑</span>';
                        html += '<span class="operation-type" onclick="deleteStore('+ d.store_id +')">删除</span>';
                        if(d.is_frozen == 1){
                            html += '<span class="operation-type" onclick="closeStore('+ d.store_id +', '+ d.is_frozen +')">开启</span>';
                        }else{
                            html += '<span class="operation-type" onclick="closeStore('+ d.store_id +', '+ d.is_frozen +')">停业</span>';
                        }

                        if(store_exit == 1){
                            html += '<button class="layui-btn layui-btn-primary ns-text-color ns-border-color" onclick="joinStore()">进入门店</button>';
                        }

                        html += '</div>';
                        html += '</div>';
                    }

                    $("#store_lists").append(html);
                }
            }
        });
    }

    function pageList(count, limit, curr, status, type) {
        if (!count) return false;
        page = new Page({
            elem: 'list_page',
            count: count,
            limit: limit,
            curr: curr,
            callback: function(obj){
                getStoreLists(search_text, obj.page, status, type);
            }
        });
    }

    function joinStore(){
        window.open("{:addon_url('store://store/login/login')}");
        //location.href="{:addon_url('store://store/login/login')}";
    }

    function editStore(data) {
        location.href = ns.url("shop/store/editStore",{"store_id":data});
    }

    function deleteStore(data){
        layer.confirm('门店已开始运营，确认要删除吗?', function() {
            $.ajax({
                url: ns.url("shop/store/deleteStore"),
                data: { store_id : data},
                dataType: 'JSON',
                type: 'POST',
                success: function(res) {
                    layer.msg(res.message);
                    repeat_flag = false;

                    if (res.code == 0) {
                        getStoreLists('', 1);
                    }
                }
            });
        });
    }

    function closeStore(store_id, is_frozen){
        var msg = '门店已开始运营，确认要关闭吗?';
        if(is_frozen == 1) {
            msg = '确定要开启该门店吗？';
        }
        layer.confirm(msg, function() {
            $.ajax({
                url: ns.url("shop/store/frozenStore"),
                data: {store_id:store_id, is_frozen:is_frozen},
                dataType: 'JSON',
                type: 'POST',
                success: function(res) {
                    layer.msg(res.message);
                    repeat_flag = false;

                    if (res.code == 0) {
                        getStoreLists('', 1);
                    }
                }
            });
        });
    }

</script>

{/block}